package com.ccepc.algmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ccepc.algmanage.service.IdentificationRecordService;
import com.ccepc.algmanage.utils.AuthInfoUtils;
import com.ccepc.common.core.util.R;
import com.ccepc.common.log.annotation.SysLog;
import com.ccepc.common.security.annotation.Inner;
import com.ccepc.common.security.service.PigxUser;
import com.ccepc.demo.api.dto.IdentificationRecordDto;
import com.ccepc.demo.api.entity.IdentificationRecord;
import com.ccepc.demo.api.entity.LinkVideo;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;


/**
 *
 *
 * @author pigx code generator
 * @date 2023-11-16 09:18:26
 */
@RestController
@RequestMapping("/identificationrecord" )
@Api(value = "identificationrecord", tags = "识别记录")
public class IdentificationRecordController {

    @Resource
    private IdentificationRecordService identificationRecordService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/page" )
    @PreAuthorize("@pms.hasPermission('algmanage_identificationrecord_view')" )
    public R getIdentificationRecordPage(@RequestBody IdentificationRecordDto identificationRecordDto) {
        return R.ok(identificationRecordService.pageWithDateRage(identificationRecordDto));
    }

    @ApiOperation(value = "查询当前月份的识别次数")
    @GetMapping("/{algorimId}" )
    @PreAuthorize("@pms.hasPermission('algmanage_identificationrecord_view')" )
    public R getById(@PathVariable("algorimId" ) Integer algorimId) {
        LocalDate currentDate = LocalDate.now();
        return R.ok(identificationRecordService.count(
                new LambdaQueryWrapper<IdentificationRecord>()
                        .eq(IdentificationRecord::getAlgorimId, algorimId)
                        .apply("YEAR(created_time) = {0} AND MONTH(created_time) = {1}", currentDate.getYear(), currentDate.getMonthValue())
        ));
    }

    @ApiOperation(value = "接收视频信息")
    @SysLog("接收视频信息" )
    @PostMapping("/videoBack")
    @Inner(value = false)
    public R saveLinkedVideo(@RequestBody LinkVideo linkVideo) {
        if (linkVideo == null || linkVideo.getVideoUrl() == null) {
            return R.failed("发送字段缺失");
        }
        return identificationRecordService.saveVideo(linkVideo);
    }

    @ApiOperation(value = "识别记录处理")
    @SysLog("识别记录处理" )
    @PostMapping("/handleRecord")
    @Inner(value = false)
    public R handleRecord(@RequestBody List<IdentificationRecord> identificationRecords) {
        return identificationRecordService.updateRemark(identificationRecords);
    }

//    @PostMapping("/insertId")
//    @Inner(value = false)
//    public R handleRecord() {
//        return R.ok(identificationRecordService.update(
//                new LambdaUpdateWrapper<IdentificationRecord>()
//                        .isNull(IdentificationRecord::getTenantId)
//                        .set(IdentificationRecord::getTenantId, 62)
//        ));
//    }

}
